package com.situ.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.admin.bean.GoodsSort;
import com.situ.admin.mapper.GoodsSortMapper;
import com.situ.admin.service.GoodsSortService;

@Service
public class GoodsSortServiceImpl implements GoodsSortService {

	@Autowired
	private GoodsSortMapper goodsSortMapper;

	@Override
	public GoodsSort add(GoodsSort goodsSort) {
		if (goodsSort.getPicture().length() == 0) {
			goodsSort.setPicture(null);
		}
		goodsSortMapper.insert(goodsSort);
		return goodsSortMapper.selectByPrimaryKey(goodsSort.getId());
	}

	@Override
	public GoodsSort remove(Integer id) {
		GoodsSort selectgoodSort = goodsSortMapper.selectByPrimaryKey(id);
		if (selectgoodSort.getStatus() == 0) {
			selectgoodSort.setStatus(1);// 软删除
		} else if (selectgoodSort.getStatus() == 1) {
			selectgoodSort.setStatus(0);
		}

		goodsSortMapper.updateByPrimaryKey(selectgoodSort);
		return selectgoodSort;
	}

	@Override
	public GoodsSort edit(GoodsSort goodsSort) {
		goodsSortMapper.updateByPrimaryKey(goodsSort);
		return goodsSortMapper.selectByPrimaryKey(goodsSort.getId());
	}

	@Override
	public PageInfo getByPage(Integer page, Integer limit, GoodsSort goodsSort) {
		if (page == null) {
			page = 1;
		}
		if (limit == null) {
			List list = goodsSortMapper.select(goodsSort);
			limit = list.size();
		}
		PageHelper.startPage(page, limit);
		List list = goodsSortMapper.select(goodsSort);
		PageInfo pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

}
